require("../../public/css/common.css")
require("../../public/css/normalize.css")
require("../../libs/fonts/iconfont.css")
require("./registar.css")


// 引入axios
const axios = require("axios");

// 验证码页面
const CaptchaMini = require("captcha-mini")
window.addEventListener("DOMContentLoaded", function () {
    let captcha1 = new CaptchaMini();
    let CaptchaMini1
    captcha1.draw(document.querySelector('#captcha1'), r => {
        CaptchaMini1 = r
    });

    // 用户输入手机号时候,光标离开,需要验证手机号是否被注册
    let tel = document.querySelector(".tel")
    // 注册失去焦点事件
    tel.addEventListener("blur", function () {
        // 判断用户是否输入手机号
        if (!/^1[3-9]\d{9}$/.test(tel.value)) {
            tel.style.border = "2px solid red";
            return
        }
        // 发生axios请求验证手机号是否被注册
        axios.post("http://47.96.154.185:3701/api/user/isExist", {
            "account": tel.value
        })
            .then(function (re) {
                // console.log(re.data);
                if (re.data.errno === 10002) {
                    document.querySelector(".hint").innerHTML = re.data.message
                    return
                } else {
                    document.querySelector(".hint").innerHTML = ""
                }
            })

    })
    // 给buttn添加点击事件,需要对表单中的数据进行验证,将数据交给后台
    let btn = document.querySelector("button")
    // 给buttn添加点击事件
    btn.addEventListener("click", function () {
        // 拿到页面上所有的输入框
        let tel = document.querySelector(".tel")
        let yanzheng = document.querySelector(".yanzheng")
        let passWord = document.querySelector(".passWord")
        let passWord2 = document.querySelector(".passWord2")
        // 验证手机号
        if (!/^1[3-9]\d{9}$/.test(tel.value)) {
            document.querySelector(".hint").innerHTML = "请输入正确的手机号码"
            return
        } else {
            document.querySelector(".hint").innerHTML = "验证成功"
        }
        // 验证验证码
        if (yanzheng.value.toUpperCase() !== CaptchaMini1.toUpperCase()) {
            document.querySelector(".hint").innerHTML = "请再次输入验证码"
            return
        }
        // 验证密码
        if (passWord.value === "" || passWord2.value === "" || passWord.value !== passWord2.value) {
            document.querySelector(".hint").innerHTML = "密码不一致"
            return
        }
        // 验证全部通过后,把数据传递给后端
        axios.post("http://47.96.154.185:3701/api/user/register", {
            account: tel.value,
            password: passWord.value,
        }).then((function (re) {
            // console.log(re.data);
            location.href = "./ad.html"
        }))
    })



    



























})

